﻿Namespace Catalog
    Public Class Label
        Inherits BaseObject
        Public Property LastPublishDate As Date
        Public Property Biography As String
        Public Property Genres As List(Of Genre)
        Public Property Images As List(Of Image)

        Public Sub New()
            Me.Genres = New List(Of Genre)
            Me.Images = New List(Of Image)
        End Sub

        Public Sub New(id As Integer, name As String, slug As String)
            Me.New()
            Me.ID = id
            Me.Name = name
            Me.Slug = slug
        End Sub

        Public Shared Function FromBPObject(lab As BPOLabel) As Label
            If lab Is Nothing Then Return Nothing
            Dim newLabel As New Label()

            newLabel.ID = lab.id
            newLabel.Name = lab.name
            newLabel.Slug = lab.slug
            Date.TryParseExact(lab.lastPublishDate, "yyyy-MM-dd", Nothing, Globalization.DateTimeStyles.None, newLabel.LastPublishDate)
            newLabel.Biography = lab.biography

            If lab.genres IsNot Nothing Then
                For Each g As BPOGenreBase In lab.genres
                    Dim newGenre As New Genre()
                    newGenre.ID = g.id
                    newGenre.Name = g.name
                    newGenre.Slug = g.slug
                    newLabel.Genres.Add(newGenre)
                Next
            End If

            If lab.images IsNot Nothing Then
                For Each img As KeyValuePair(Of String, BPOImage) In lab.images
                    Dim newImage As Image = Image.FromBPObject(img)
                    newLabel.Images.Add(newImage)
                Next
            End If

            Return newLabel
        End Function
    End Class
End Namespace